/**
 * 
 */
package xx.teach.entity;

import java.sql.SQLException;
import java.sql.Timestamp;

import javax.sql.RowSet;

import xx.db.QueryRunner;
import xx.db.annotation.Column;
import xx.db.annotation.TableName;
import xx.db.sql.Owner;
import xx.db.sql.Table;

/**
 * @author Administrator 2014-3-18
 */
@TableName(name = "t_chat")
public class Chat extends Table
{

    /**
     * @param qr
     * @param owner
     */
    public Chat(QueryRunner qr, Owner owner)
    {
        super(qr, owner);
        // TODO Auto-generated constructor stub
    }

    public static final String CHAT_ID = "chat_id";

    @Column(name = CHAT_ID, pk = true)
    private long chatId;

    public static final String GROUP_ID = "group_id";

    @Column(name = GROUP_ID)
    private long groupId;

    public static final String ACCOUNT_ID = "account_id";

    @Column(name = ACCOUNT_ID)
    private long accountId;

    public static final String CONTENT = "content";

    @Column
    private String content;

    public static final String SUBMIT_TIME = "submit_time";

    @Column(name = SUBMIT_TIME)
    private Timestamp submitTime;

    public static final String SEQUENCE = "sequence";

    @Column
    private int sequence;

    public static final String ATTACH_PATH = "attach_path";

    @Column(name = ATTACH_PATH)
    private String attachPath;

    public long getChatId()
    {
        return chatId;
    }

    public Chat setChatId(long chatId)
    {
        this.chatId = chatId;
        return this;
    }

    public long getGroupId()
    {
        return groupId;
    }

    public Chat setGroupId(long groupId)
    {
        this.groupId = groupId;
        return this;
    }

    public String getContent()
    {
        return content;
    }

    public Chat setContent(String content)
    {
        this.content = content;
        return this;
    }

    public Timestamp getSubmitTime()
    {
        return submitTime;
    }

    public Chat setSubmitTime(Timestamp submitTime)
    {
        this.submitTime = submitTime;
        return this;
    }

    public int getSequence()
    {
        return sequence;
    }

    public Chat setSequence(int sequence)
    {
        this.sequence = sequence;
        return this;
    }

    public String getAttachPath()
    {
        return attachPath;
    }

    public Chat setAttachPath(String attachPath)
    {
        this.attachPath = attachPath;
        return this;
    }

    public long getAccountId()
    {
        return accountId;
    }

    public Chat setAccountId(long accountId)
    {
        this.accountId = accountId;
        return this;
    }

    /*
     * (non-Javadoc)
     * @see xx.db.sql.Table#insert()
     */
    @Override
    public long insert() throws SQLException
    {
        int sequence = getNextSequence()+1;
        this.setSequence(sequence);
        return super.insert();
    }

    /**
     * 获取sequence
     * 
     * @return
     * @throws SQLException
     */
    private int getNextSequence() throws SQLException
    {
        RowSet rs = select.from(Chat.class, "max(" + Chat.SEQUENCE + ")").where(Chat.GROUP_ID + "=" + groupId).query();
        if (rs.next())
            return rs.getInt(1);
        else
            return 1;
    }
}
